﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace IRCBot
{
    public partial class ConsoleLog : Form
    {

        Thread cT; // IRC
        Thread gT; // Glider

        TcpClient ircClient;
        NetworkStream ircStream;
        StreamWriter ircOutput;
        StreamReader ircInput;

        TcpClient gClient;
        NetworkStream gStream;
        StreamWriter gOutput;
        StreamReader gInput;

        string ircServer = "irc.quakenet.org";      // IRC Server
        int ircPort = 6667;                         // Server Port
        string ircChannel = "#channel";             // Channel
        bool ircHidehost = false;                   // Mask your host / Hide your IP?
        string ircNick = "";                        // Nickname
        string ircAuthName = "";                    // QuakeNets authenication system
        string ircAuthPass = "";                    // Password
        string ircAdminNick = "";                   // Your master

        string gIP = "";                            // Glider IP
        int gPort = ;                               // Glider Port
        string gPass = "";                          // Glider Password

        string bVersion = "0.0.4b";

        //Don't touch!

        bool Attached, gCon;
        bool gStarted = false;
        string Character, Class, Target, Version, Level, Bars;
        int Mana, XP, NextXP;
        double Health;

        public ConsoleLog()
        {
            InitializeComponent();
            Log("IRC bot initialized! Version " + bVersion + " loaded - created by miceiken");
            tbversion.Text = bVersion;
        }

        private delegate void LogHandler(string msg);
        private delegate void IRCInitHandler();
        private delegate void JoinHandler(string channel);
        private delegate void MsgHandler(string dest, string msg);
        private delegate void CmdHandler(string cmd);
        
        public void Log(string msg)
        {
            if (!InvokeRequired)
            {
                LogBox.Items.Add("[" + DateTime.Now.ToString("HH:mm:ss") + "]  " + msg);
                int itemsPerPage = (int)(LogBox.Height / LogBox.ItemHeight);
                LogBox.TopIndex = LogBox.Items.Count - itemsPerPage;
            }
            else
            {
                Invoke(new LogHandler(Log), msg);
            }
        }

        void IRCInit()
        {
            if (!InvokeRequired)
            {
                Auth(ircAuthName, ircAuthPass);
                Join(ircChannel);
                ConnectGlider(gIP, gPort);
            }
            else
            {
                Invoke(new IRCInitHandler(IRCInit));
            }
        }

        void Join(string channel)
        {
            if (!InvokeRequired)
            {
                ircOutput.WriteLine("JOIN " + channel);
                ircOutput.Flush();
            }
            else
            {
                Invoke(new JoinHandler(Join), channel);
            }
        }

        void Auth(string user, string pass)
        {
            ircOutput.WriteLine("AUTH " + user + " " + pass);
            if (ircHidehost)
            { // Hide your IP?
                ircOutput.WriteLine("MODE " + ircNick + " +x");
            }
            ircOutput.Flush();
        }

        void Message(string dest, string msg)
        { // Simple function to send message
            if (!InvokeRequired)
            {
                ircOutput.WriteLine("PRIVMSG " + dest + " :" + msg);
                ircOutput.Flush();
            }
            else
            {
                Invoke(new MsgHandler(Message), dest, msg);
            }
        }

        void gCommand(string cmd)
        {
            if (!InvokeRequired)
            {
                gOutput.WriteLine(cmd);
                gOutput.Flush();
            }
            else
            {
                Invoke(new CmdHandler(gCommand), cmd);
            }
        }

        private void tbcon_Click(object sender, EventArgs e)
        {
            if (!gStarted)
            {
                this.ConnectIRC(ircServer, ircPort);
                this.tbcon.Text = "Disconnect";
                this.tbcon.Image = global::miceIRC.Properties.Resources.control_stop_blue;
                this.ssstatus.Image = global::miceIRC.Properties.Resources.tick;
                this.ssstatus.Text = "Connecting";
                gStarted = true;
            }
            else
            {
                this.gClient.Close();
                this.tbcon.Text = "Connect";
                this.tbcon.Image = global::miceIRC.Properties.Resources.control_play_blue;
                this.ssstatus.Image = global::miceIRC.Properties.Resources.cross;
                this.ssstatus.Text = "Disconnected";
                gStarted = false;
            }
        }

        private void tbconfig_Click(object sender, EventArgs e)
        {
        }
    }
}